package com.zucc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.zucc.controller.req.ExperimentAddReq;
import com.zucc.controller.req.ExperimentModifyReq;
import com.zucc.controller.req.ExperimentSearchReq;
import com.zucc.controller.resp.ExperimentResp;
import com.zucc.entity.Attachment;
import com.zucc.entity.Category;
import com.zucc.entity.Experiment;
import com.zucc.entity.ExperimentMapper;
import com.zucc.entity.base.MyMapper;
import com.zucc.exception.SyzxException;
import com.zucc.service.ExperimentService;
import com.zucc.service.base.MyServiceImpl;
import com.zucc.service.base.PageData;
import com.zucc.util.MyPageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author : charspan
 * @Description :
 * @create : 2021-12-11 8:56 下午
 */
@Service
public class ExperimentServiceImpl extends MyServiceImpl<Experiment> implements ExperimentService {


    @Resource
    private ExperimentMapper experimentMapper;

    @Override
    public MyMapper<Experiment> mapper() {
        return experimentMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ExperimentAddReq req) {
        Experiment experiment = new Experiment();
        BeanUtil.copyProperties(req, experiment);
        experiment.setDelFlag(0);
        experiment.setCreateTime(DateUtil.date());
        experiment.setModifyTime(DateUtil.date());
        experiment.setApprovalStatus(0);
        super.saveKey(experiment);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modify(ExperimentModifyReq req) {
        Experiment experimentExist = super.findById(req.getId());
        if (Objects.isNull(experimentExist) || experimentExist.getDelFlag().intValue() == 1) {
            throw new SyzxException("系统找不到相应编号的实验项目卡信息");
        }
        BeanUtil.copyProperties(req, experimentExist, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        experimentExist.setModifyTime(DateUtil.date());
        super.updateSelective(experimentExist);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteOne(Long id) {
        Experiment experimentExist = super.findById(id);
        if (Objects.isNull(experimentExist) || experimentExist.getDelFlag().intValue() == 1) {
            throw new SyzxException("系统找不到相应编号的实验项目卡信息");
        }
        experimentExist.setDelFlag(1);
        experimentExist.setDeleteTime(DateUtil.date());
        super.updateSelective(experimentExist);
    }

    @Override
    public PageData<ExperimentResp> page(ExperimentSearchReq req) {
        Example example = new Example(Experiment.class);
        example.setOrderByClause("modify_time DESC");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("delFlag", 0);
        if (Objects.nonNull(req.getApprovalStatus())) {
            criteria.andEqualTo("approvalStatus", req.getApprovalStatus());
        }
        if (StrUtil.isNotBlank(req.getRemarks())) {
            criteria.andLike("remarks", "%" + req.getRemarks() + "%");
        }
        if (StrUtil.isNotBlank(req.getLaboratoryName())) {
            criteria.andLike("laboratoryName", "%" + req.getLaboratoryName() + "%");
        }
        if (StrUtil.isNotBlank(req.getCode())) {
            criteria.andLike("code", "%" + req.getCode() + "%");
        }
        if (StrUtil.isNotBlank(req.getName())) {
            criteria.andLike("name", "%" + req.getName() + "%");
        }
        if (StrUtil.isNotBlank(req.getCategory())) {
            criteria.andLike("category", "%" + req.getCategory() + "%");
        }
        if (StrUtil.isNotBlank(req.getType())) {
            criteria.andLike("type", "%" + req.getType() + "%");
        }
        if (StrUtil.isNotBlank(req.getClassName())) {
            criteria.andLike("className", "%" + req.getClassName() + "%");
        }
        if (StrUtil.isNotBlank(req.getMajor())) {
            criteria.andLike("major", "%" + req.getMajor() + "%");
        }
        if (StrUtil.isNotBlank(req.getSubject())) {
            criteria.andLike("subject", "%" + req.getSubject() + "%");
        }
        if (StrUtil.isNotBlank(req.getIndependent())) {
            criteria.andLike("independent", "%" + req.getIndependent() + "%");
        }
        PageData<Experiment> pageData = super.findByPage(req, example);
        PageData<ExperimentResp> convert = MyPageUtil.convert(pageData, ExperimentResp.class);
        return convert;
    }
}
